package lxh.listdatascreenview.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Created by liuxh on 2017/7/30.
 */

public class ListDataScreenView extends LinearLayout {
    private static final String TAG = "ListDataScreenView";
    private LinearLayout mMenuContainerView;//装菜单的容器
    private FrameLayout mMenuSpinnerContainerView;//装菜单下拉显示的内容及阴影的容器

    private View mShadowView;//阴影
    private FrameLayout mContentContainerView;//下拉显示内容的容器

    private int mShadowColor = 0x88888888;//阴影背景色
    private float mContentContainerHeight;//内容布局的高度

    private Context mContext;
    private BaseMenuAdapter mAdapter;
    private int mCurrentMenuPosition = -1;//当前菜单位置
    private final long DURATION_TIME = 500;//动画时间

    private boolean mIsAnimatorExecute = false;

    public ListDataScreenView(Context context) {
        this(context, null);
    }

    public ListDataScreenView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ListDataScreenView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(TAG, "onMeasure: ");
        //计算内容容器的高度
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (height > 0 && mContentContainerHeight == 0) {
            mContentContainerHeight = height * 75f / 100;
            ViewGroup.LayoutParams params = mContentContainerView.getLayoutParams();
            params.height = (int) mContentContainerHeight;
            mContentContainerView.setLayoutParams(params);
            //初始状态内肉是不显示的
            mContentContainerView.setTranslationY(-mContentContainerHeight);
        }

    }

    private void initLayout() {
        setOrientation(VERTICAL);
        //将菜单容器添加进来
        mMenuContainerView = new LinearLayout(mContext);
        LayoutParams linearLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mMenuContainerView.setLayoutParams(linearLayoutParams);
        addView(mMenuContainerView);

        //添加阴影跟内容到mMenuSpinnerContainerView容器
        mMenuSpinnerContainerView = new FrameLayout(mContext);
        LayoutParams menuSpinnerParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                0);
        menuSpinnerParams.weight = 1;
        mMenuSpinnerContainerView.setLayoutParams(menuSpinnerParams);
        //添加阴影
        mShadowView = new View(mContext);
        mShadowView.setBackgroundColor(mShadowColor);
        setShadowViewClick();
        mShadowView.setVisibility(GONE);//默认不显示
        mMenuSpinnerContainerView.addView(mShadowView);
        //添加内容布局
        mContentContainerView = new FrameLayout(mContext);
        mContentContainerView.setBackgroundColor(Color.WHITE);
        mMenuSpinnerContainerView.addView(mContentContainerView);
        //将下拉容器添加进来
        addView(mMenuSpinnerContainerView);

    }

    /**
     * 阴影点击事件
     */
    private void setShadowViewClick() {
        mShadowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closeMenu();
            }
        });

    }

    public void setAdapter(BaseMenuAdapter adapter) {
        mAdapter = adapter;
        if (mAdapter == null) {
            throw new NullPointerException("adapter can not be null");
        }

        int count = adapter.getCount();
        for (int i = 0; i < count; i++) {
            //添加菜单view
            View tabView = mAdapter.getTabView(mMenuContainerView, i);
            mMenuContainerView.addView(tabView);
            LinearLayout.LayoutParams layoutParams = (LayoutParams) tabView.getLayoutParams();
            layoutParams.weight = 1;
            tabView.setLayoutParams(layoutParams);
            //设置菜单点击事件
            setTabViewOnClick(tabView, i);
            //添加内容view
            View menuContentView = mAdapter.getMenuContentView(mContentContainerView, i);
            menuContentView.setVisibility(GONE);
            mContentContainerView.addView(menuContentView);
        }
    }

    /**
     * 菜单点击事件
     *
     * @param view
     */
    private void setTabViewOnClick(final View view, final int position) {
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentMenuPosition == -1) {
                    //表示未打开下拉菜单,则打开
                    openMenu(view, position);

                } else {
                    //已打开，则关闲
                    if (mCurrentMenuPosition == position) {
                        //当前菜单是打开状态，再次点击当前位置，则关闭当前菜单
                        closeMenu();
                    } else {
                        //切换显示内容
                        View curContentView = mContentContainerView.getChildAt(mCurrentMenuPosition);
                        curContentView.setVisibility(GONE);
                        mAdapter.setCloseMenuState(mMenuContainerView.getChildAt(mCurrentMenuPosition));//改变菜单状态
                        mCurrentMenuPosition = position;
                        curContentView = mContentContainerView.getChildAt(mCurrentMenuPosition);
                        curContentView.setVisibility(VISIBLE);
                        mAdapter.setOpenMenuState(mMenuContainerView.getChildAt(mCurrentMenuPosition));

                    }

                }
            }
        });
    }

    /**
     * 关闭菜单
     */
    private void closeMenu() {
        if (mIsAnimatorExecute) return;
        //内容是位移动画
        final ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(mContentContainerView, "translationY", 0, -mContentContainerHeight);
        //阴影透明动画
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mShadowView, "alpha", 1f, 0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(DURATION_TIME);
        animatorSet.playTogether(translationAnimator, alphaAnimator);

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mShadowView.setVisibility(GONE);
                View contentView = mContentContainerView.getChildAt(mCurrentMenuPosition);
                contentView.setVisibility(GONE);
                mCurrentMenuPosition = -1;
                mIsAnimatorExecute = false;

            }

            @Override
            public void onAnimationStart(Animator animation) {
                mIsAnimatorExecute = true;
                mAdapter.setCloseMenuState(mMenuContainerView.getChildAt(mCurrentMenuPosition));
            }
        });
        animatorSet.start();


    }

    /**
     * 打开菜单
     *
     * @param view
     * @param position
     */
    private void openMenu(View view, final int position) {
        if (mIsAnimatorExecute) return;
        mShadowView.setVisibility(VISIBLE);
        View contentView = mContentContainerView.getChildAt(position);//当前内容view
        contentView.setVisibility(VISIBLE);

        //内容是位移动画
        ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(mContentContainerView, "translationY", -mContentContainerHeight, 0);
        //阴影透明动画
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mShadowView, "alpha", 0f, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(DURATION_TIME);
        animatorSet.playTogether(translationAnimator, alphaAnimator);

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mCurrentMenuPosition = position;
                mIsAnimatorExecute = false;
            }

            @Override
            public void onAnimationStart(Animator animation) {
                mIsAnimatorExecute = true;
                mAdapter.setOpenMenuState(mMenuContainerView.getChildAt(position));
            }
        });
        animatorSet.start();
    }
}
